package cn.jarlen.jwt.tools.antivirus;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.jarlen.jwt.tools.menu.IMenuProcess;
import jxl.Sheet;
import jxl.Workbook;

/**
 * @author jarlen
 * Create by 2024/7/3 8:57
 */
public class ReadExcel implements IMenuProcess {
    @Override
    public void onExecute(Context context) {
        Log.w("ReadExcel", "读取Excel 开始");
        List<CVELib> antivirusList = readExcel(context.getApplicationContext());
        Log.w("ReadExcel", "读取Excel 完成:" + JSON.toJSONString(antivirusList));
        AntivirusDao.getInstance(context).insertAntivirus(antivirusList);
        Log.w("ReadExcel", "漏洞库入库 完成");
    }

    public static List<CVELib> readExcel(Context context) {
        List<CVELib> antivirusList = new ArrayList<>();
        try {
            InputStream is = context.getAssets().open("HuaWei_Antivirus.xls");
            Workbook book = Workbook.getWorkbook(is);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int rowCount = sheet.getRows();

            for (int index = 1; index < rowCount; ++index) {
                String cve = (sheet.getCell(0, index)).getContents();
                String cve_desc = (sheet.getCell(1, index)).getContents();
                String cve_desc2 = (sheet.getCell(2, index)).getContents();
                String patchVer = (sheet.getCell(3, index)).getContents();
                String level = (sheet.getCell(4, index)).getContents();

                String version = (sheet.getCell(5, index)).getContents();
                version = version.replaceAll("HarmonyOS", "")
                        .replaceAll(" ", "")
                        .trim();

                CVELib antivirus = new CVELib();
                antivirus.setCve(cve);
                antivirus.setCveDesc(cve_desc + cve_desc2);
                antivirus.setPatchVer(patchVer);
                antivirus.setCveLevel(level);
                antivirus.setAffectedVersion(version);
                antivirus.setSystem("HarmonyOS");

                antivirusList.add(antivirus);
            }
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return antivirusList;
    }
}
